package com.yonyou.gmmc.service.getCar.service;

import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.FrameHttpUtil;
import org.marketing.common.util.CheckUtil;
import org.marketing.common.util.CommonUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class IdealerssService {
	private final Logger logger = Logger.getLogger(IdealerssService.class);
	
	@Value("${IDEALERSS_URL}")
	private String idealerurl;
	@Value("${SECRET_KEY}")
	private String secretKey;
	@Value("${CHANNEL}")
	private String channel;
	
	/**
	 * 向idealer下单,积分支付
	 * 
	 * @param param
	 */
	public String sendOrder(Map<String, Object> para) throws Exception{
		para.put("channel", channel);
		logger.info("参数：" + para);
		String jsonResponse = FrameHttpUtil.post1(idealerurl+"api/order/add", CommonUtils.generateSign(para, secretKey));
		logger.info("jsonResponse="+jsonResponse);
		return jsonResponse;
	}
	/**
	 * 向idealer下单,微信支付
	 * 
	 * @param param
	 */
	public String wxOrder(Map<String, Object> para) throws Exception{
		para.put("channel", channel);
		logger.info("参数：" + para);
		String jsonResponse = FrameHttpUtil.post1(idealerurl+"api/order/pre", CommonUtils.generateSign(para, secretKey));
		logger.info("jsonResponse="+jsonResponse);
		return jsonResponse;
	}
	
	/**
	 * 查询司机信息
	 * 
	 * @param para
	 * @return
	 * @throws Exception
	 */
	public String driverInfo(Map<String, Object> para) throws Exception{
		para.put("channel", channel);
		logger.info("参数：" + para);
		String jsonResponse = FrameHttpUtil.post1(idealerurl+"api/driver/jsonInfo?", CommonUtils.generateSign(para, secretKey));
		logger.info("jsonResponse="+jsonResponse);
		return jsonResponse;
	}
	/**
	 * 验证订单
	 * 
	 * @param para
	 * @return
	 * @throws Exception
	 */
	public String validOrder(Map<String, Object> para) throws Exception{
		para.put("channel", channel);
		logger.info("参数：" + para);
		String jsonResponse = FrameHttpUtil.post1(idealerurl+"api/order/valid", CommonUtils.generateSign(para, secretKey));
		logger.info("jsonResponse="+jsonResponse);
		return jsonResponse;
	}
	
	/**
	 * 获取订单状态
	 * 
	 * @param para
	 * @return
	 * @throws Exception
	 */
	public String searchOrder(Map<String, Object> para) throws Exception{
		logger.info("参数：" + para);
	    String jsonResponse = FrameHttpUtil.post1(idealerurl+"api/order/search", CommonUtils.generateSign(para, secretKey));
		logger.info("jsonResponse="+jsonResponse);
		return jsonResponse;
	}
	
	/**
	 * 取消订单
	 * 
	 * @param para
	 * @return
	 * @throws Exception
	 */
	public String cancelOrder(Map<String, Object> para) throws Exception{
		para.put("channel", channel);
		logger.info("参数：" + para);
		String jsonResponse = FrameHttpUtil.post1(idealerurl+"api/order/cancel", CommonUtils.generateSign(para, secretKey));
		logger.info("jsonResponse="+jsonResponse);
		return jsonResponse;
	}
	
	
	/**
	 * 订单轨迹
	 * 
	 * @param para
	 * @return
	 * @throws Exception
	 */
	public String trackOrder(Map<String, Object> para) throws Exception{
		para.put("channel", channel);
		logger.info("参数：" + para);
		String jsonResponse = FrameHttpUtil.post1(idealerurl+"api/order/track", CommonUtils.generateSign(para, secretKey));
		logger.info("jsonResponse="+jsonResponse);
		return jsonResponse;
	}

    /**
     * 
     * 功能描述: <br>
     * 获取取送车接车验证码
     *
     * @param para
     * @return
     * @throws Exception
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public String getVerifyCode(Map<String, Object> para) throws Exception {
        para.put("channel", channel);
        logger.info("参数：" + para);
        String jsonResponse = FrameHttpUtil.sendGet(idealerurl+"api/order/verifyCode?", getParamsStr(CommonUtils.generateSign(para, secretKey)));
        logger.info("jsonResponse="+jsonResponse);
        return jsonResponse;
    }
    
    /**
     * 
     * 功能描述: <br>
     * get请求参数
     *
     * @param params
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private String getParamsStr(Map<String, Object> params){
        String getParams = "";
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            getParams = getParams + "&" + entry.getKey() + "=" + entry.getValue();
        }
        if(!CheckUtil.NullOrEmpty(getParams)){
            getParams = getParams.substring(1);
        }
        logger.info("getParams: " + getParams);
        return getParams;
    }
}
